package com.dd.front.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dd.common.dto.user.UserLoginDTO;
import com.dd.common.dto.user.UserRegisterDTO;
import com.dd.common.entity.User;
import com.dd.common.enums.ErrorEnum;
import com.dd.common.exception.BusinessException;
import com.dd.common.service.UserService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.StrGenerator;
import com.dd.common.vo.UserLoginVO;
import com.dd.front.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author cheng
 */
@SaIgnore
@Tag(name = "登录")
@RestController
@RequestMapping()
public class LoginController {
    @Resource
    private LoginService loginService;
    @Resource
    private UserService userService;
    @Operation(summary = "用户登录", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "登录数据",
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UserLoginDTO.class))
    ))
    @PostMapping("/account/login")
    public AjaxResult<UserLoginVO> login(@Validated @RequestBody UserLoginDTO dto) {
        return AjaxResult.success(loginService.accountLogin(dto));
    }

    @Operation(summary = "用户注册", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "注册数据",
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UserRegisterDTO.class))
    ))
    @PostMapping("/account/register")
    public AjaxResult<Boolean> register(@Validated @RequestBody UserRegisterDTO dto) {
        User user = userService.getOne(new LambdaQueryWrapper<>(User.class).eq(User::getUsername, dto.getUsername()), false);
        if (user != null) {
           throw new BusinessException(ErrorEnum.DATA_EXISTS, "用户已存在");
        }
        User newUser = new User();
        newUser.setUsername(dto.getUsername());
        newUser.setPassword(StrGenerator.makeMd5(dto.getPassword()));
        newUser.setNickname(dto.getUsername());
        newUser.setAvatar(StrGenerator.generateRandomAvatar());
        return AjaxResult.success(userService.save(newUser));
    }

    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    public AjaxResult<Object> logout() {
        StpUtil.logout();
        return AjaxResult.success();
    }

}
